package com.lemon.stock.compose

import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.material.*
//import androidx.compose.material.R
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.ArrowBack
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.input.KeyboardType
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.lifecycle.viewmodel.compose.viewModel
import androidx.navigation.NavHostController
import com.lemon.stock.R
import com.lemon.stock.compose.mine.*
import com.lemon.stock.theme.back007
import com.lemon.stock.viewmodel.LoginViewModel

@Composable
fun LoginScreen(navController: NavHostController){
    val mViewModel: LoginViewModel = viewModel()
    Scaffold(
        topBar = {
            TopAppBar(
                title = {Text(text="登录")},
                navigationIcon = {
                    IconButton(onClick = {  }) {
                        Icon(Icons.Filled.ArrowBack, contentDescription = null)
                    }
                }
            )
        }
    ) {
        Column(
            verticalArrangement = Arrangement.Top,
            modifier = Modifier.fillMaxSize()
        ) {
            Spacer(modifier = Modifier.height(60.dp))
            Row(){
                OutlinedTextField(
                    value = mViewModel.username,
                    onValueChange = { value ->
                        mViewModel.username = value
                    },
                    modifier = Modifier.fillMaxWidth().padding(vertical = 4.dp),
                    label = {
                        Text(text = stringResource(id = R.string.user_username))
                    },
                    keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Password),
                    singleLine = true,
                    colors = TextFieldDefaults.outlinedTextFieldColors(
                        focusedBorderColor = Color.Transparent,
                        unfocusedBorderColor = Color.Transparent
                    )
                )
            }
            Divider(modifier = Modifier.padding(horizontal = 16.dp), thickness = 0.5.dp)
            Spacer(modifier = Modifier.height(10.dp))
            Row(){
                OutlinedTextField(
                    value = mViewModel.password,
                    onValueChange = { value ->
                        mViewModel.password = value
                    },
                    modifier = Modifier.fillMaxWidth().padding(vertical = 4.dp),
                    label = {
                        Text(text = stringResource(id = R.string.user_password))
                    },
                    keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Password),
                    singleLine = true,
                    colors = TextFieldDefaults.outlinedTextFieldColors(
                        focusedBorderColor = Color.Transparent,
                        unfocusedBorderColor = Color.Transparent
                    )
                )
            }
            Divider(modifier = Modifier.padding(horizontal = 16.dp), thickness = 0.5.dp)
            Spacer(modifier = Modifier.height(30.dp))

            Button(
                onClick = { mViewModel.login(navController) },
                modifier = Modifier
                    .fillMaxWidth()
                    .height(44.dp)
                    .padding(horizontal = 16.dp),
                shape = RoundedCornerShape(percent = 50),
            ) {
                Text(text = "登录", fontSize = 15.sp)
            }
            Spacer(modifier = Modifier.height(20.dp))
            Text(
                text = "没有账号？去注册",
                modifier = Modifier
                    .align(Alignment.End)
                    .padding(horizontal = 16.dp)
                    .clickable {
                        navController.navigate("register")
                    },
                color = back007,
                fontSize = 15.sp
            )

        }
    }
}